<template>
    <div 
        :style="{ 
            flexDirection: isMy ? 'row-reverse' : 'row', 
            marginBottom: '0', 
            marginTop: '0', 
            display: 'flex',
            alignItems: 'flex-start'
        }"
    >
      <img :src="headUrl" class="chat-avatar" v-on:click="avatarClickEvent" />
      <div class="message-container">
        <template v-if="type === 'text'">
          <div v-if="!isMy" class="nickname-container">{{ nickname }}</div>
          <div
            class="chat-text"
            :class="isMy ? 'isMyWordStyle' : 'isOtherWordStyle'"
            :click="chatTextItemClickEvent"
            :data-index="index"
          >
            {{ content }}
          </div>
          <chat-info-time v-if="showTime && time" :time="time" :isMy="isMy" />
        </template>

        <template v-if="type === 'image'">
          <div v-if="!isMy" class="nickname-container">{{ nickname }}</div>  
          <img
            class="chat-image"
            :src="content"
            @click="imageClickEvent(content)"
          />
          <chat-info-time v-if="showTime && time" :time="time" :isMy="isMy" />
        </template>

        <template v-if="type === 'voice'">
          <div v-if="!isMy" class="nickname-container">{{ nickname }}</div>  
          <div class="chat-voice" :click="chatVoiceItemClickEvent">
            <VoiceItem :isMy="isMy" :isPlaying="isPlaying" />
          </div>
          <div class="voice-duration">{{ voiceDuration }}"</div>
          <chat-info-time v-if="showTime && time" :time="time" :isMy="isMy" />
        </template>
      </div>
      <ChatSendStatus v-if="isMy" :sendStatus="sendStatus" :index="index" />
    </div>
  </template>

<script lang="ts">
import { nextTick, onMounted, ref } from 'vue';
import type { PropType } from 'vue';
import ChatSendStatus from './chat-info-send-status.vue';
import VoiceItem from './chat-info-voice.vue';
import ChatLoading from './chat-info-loading.vue';
import ChatCustom from './chat-info-custom.vue';
import ChatInfoTime from './chat-info-time.vue';
import type { MessageType } from '../im-type/im-message-type';
export default {
    name: 'chat-info-word', 
    components: {
        ChatSendStatus,
        VoiceItem,
        ChatLoading,
        ChatCustom,
        ChatInfoTime
    },
    props: {
        // 消息id
        id: {
            type: Number,
            default: 0
        },
        // 是否是自己的消息
        isMy: {
            type: Boolean,
            default: false
        },
        // 昵称
        nickname: {
            type: String,
            default: ''
        },
        // 消息索引
        index: {
            type: Number,
            default: 0
        },
        // 消息长度
        length: {
            type: Number,
            default: 0
        },
        // 头像url
        headUrl: {
            type: String,   
            default: ''
        },
        // 消息类型
        type: {
            type: String as PropType<MessageType>,
            default: ''
        },
        // 消息内容
        content: {
            type: String,
            default: ''
        },
        // 发送状态
        sendStatus: {
            type: String,
            default: ''
        },
        // 语音时长
        voiceDuration: {
            type: Number,
            default: 0
        },
        // 是否正在播放
        isPlaying: {
            type: Boolean,
            default: false
        },
        // 文本消息点击事件
        chatTextItemClickEvent: {
            type: Function,
            default: () => {}
        },
        // 图片点击事件
        imageClickEvent: {
            type: Function,
            default: () => {}
        },
        // 语音点击事件
        chatVoiceItemClickEvent: {
            type: Function,
            default: () => {}
        },
        // 是否显示时间
        showTime: {
            type: Boolean,
            default: false
        },
        // 时间
        time: {
            type: String,
            default: ''
        },
        // 头像点击事件
        avatarClick: {
            type: Function,
            default: (id: number, isMy: boolean) => {
                console.log('chat-info-word头像点击事件',id,isMy)
            }
        }
    },
    setup(props, { emit }) {
        const bubble = ref<HTMLElement | null>(null);
        const avatarClickEvent = () => {
            props.avatarClick(props.id,props.isMy)
            // emit('avatarClick',props.id,props.isMy)
        }
        onMounted(() => {
            nextTick(() => {
                if (bubble.value) {
                    emit('bubble-width', bubble.value.offsetWidth);
                }
            });
        });
        return {
            bubble,
            avatarClickEvent
        };
    }
}
</script>

<style scoped>
.message-container {
    position: relative;
    margin: 0 10px;
    max-width: 75%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 20px;
    margin-bottom: 20px;
}

.chat-text {
    position: relative;
    border-radius: 12px;
    padding: 10px 15px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    margin-bottom: 10px;
}

.isMyWordStyle {
    background-color: #8fcbf7;
    color: #213d52;
    margin-left: 6px;
}

.isMyWordStyle::after {
    content: '';
    position: absolute;
    right: -8px;
    top: 12px;
    width: 0;
    height: 0;
    border: 10px solid transparent;
    border-left-color: #8fcbf7;
    border-right-width: 0;
}

.isOtherWordStyle {
    background-color: white;
    color: #333333;
    margin-right: 6px;
}

.isOtherWordStyle::before {
    content: '';
    position: absolute;
    left: -8px;
    top: 12px;
    width: 0;
    height: 0;
    border: 10px solid transparent;
    border-right-color: white;
    border-left-width: 0;
    z-index: 1;
}

.chat-image {
    max-width: 60%;
    border-radius: 12px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    margin: 10px 0;
}

.chat-voice {
    background-color: #f5f5f5;
    border-radius: 20px;
    padding: 10px 15px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    margin: 10px 0;
}

.voice-duration {
    color: #666;
    font-size: 12px;
    margin-top: 4px;
}

.chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin: 0 10px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    align-self: flex-start;
    margin-top: 20px;
}

.chat-info-time {
    position: relative;
    text-align: center;
    font-size: 12px;
    color: #999;
    margin-top: 8px;
    width: 100%;
    left: 0;
    border: none;
}

.parent-container {
    overflow: visible;
}

.nickname-container {
    font-size: 12px;
    color: #666;
    margin-bottom: 4px;
    font-weight: bold;
    align-self: flex-start;
    padding-left: 4px;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
    letter-spacing: 0.5px;
}
</style>

